package com.example.anwserai.demos.web.util;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

@Slf4j
public class FileUtilQ4 {

    private static final String _XLSX_SUFFIX = ".xlsx";

    public static String redDoc (String fileName) {
        try (FileInputStream fis = new FileInputStream(fileName);
             HWPFDocument document = new HWPFDocument(fis)) {
            WordExtractor extractor = new WordExtractor(document);
            String text = extractor.getText();
            if (StringUtils.hasLength(text)) {
                return text;
            }
            throw new RuntimeException("DOC读取为空");
        } catch (Exception e) {
            throw new RuntimeException("DOC解析失败");
        }
    }

    public static String redDocx(String fileName) {
        try (FileInputStream fis = new FileInputStream(fileName);
             XWPFDocument document = new XWPFDocument(fis);
             XWPFWordExtractor extractor = new XWPFWordExtractor(document)) {
            String text = extractor.getText();
            if (StringUtils.hasLength(text)) {
                return text;
            }
            throw new RuntimeException("DOCX读取为空");
        } catch (Exception e) {
            throw new RuntimeException("DOCX解析失败");
        }
    }

    public static String redPDF(String fileName) {
        try {
            // 使用 Path 和 InputStream 加载 PDF 文档
            File file = new File(fileName);
            PDDocument document = PDDocument.load(file);

            // 创建 PDFTextStripper 对象
            PDFTextStripper stripper = new PDFTextStripper();

            // 提取文本
            String text = stripper.getText(document);
            // 关闭文档
            document.close();
            if (StringUtils.hasLength(text)) {
                if (fileName.startsWith("关于做好2024年端午期间价格监测工")) {
                    log.error("目标文件解析结果：{}", text);
                }
                return text;
            }
            if (fileName.startsWith("关于做好2024年端午期间价格监测工")) {
                log.error("目标文件解析为空");
            }
            throw new RuntimeException("PDF读取为空，可能是图片");
        } catch (Exception e) {
            if (fileName.startsWith("关于做好2024年端午期间价格监测工")) {
                log.error("目标文件解析出错");
            }
            throw new RuntimeException("PDF解析失败");
        }
    }
}
